﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using tuwien.auto.eibclient;

namespace KNXFramework.KNXObjects.ValueObjects.Signed
{
    internal class S8ObjectValue : IntObjectValue
    {
        public S8ObjectValue(string value) : base(value)
        {
            if (m_Value > 127 || m_Value < -128)
                throw new ApplicationException("S8ObjectValue: value must be between -128 and 127. Val : " + value);
        }

        public S8ObjectValue(EIBBuffer buffer)
        {
            int newValue;
            if (buffer.data.Length == 2)
                newValue = buffer.data[1] & 0x3f;
            else
                newValue = buffer.data[2];

            if (newValue > 127)
                newValue -= 256;
        }

        public override void AdaptBufferBeforeSending(bool isWrite, EIBBuffer buffer)
        {
            byte[] b = new byte[3];
            b[0] = buffer.data[0];
            b[1] = buffer.data[1];
            b[2] = (byte)(m_Value & 0xff);
            buffer.data = b;
        }
    }
}
